package com.fjwt.gz.mch.ctrl.score.record;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONObject;
import com.fjwt.gz.core.constants.Constants;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.entity.*;
import com.fjwt.gz.mch.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 福分商城
 */
@Slf4j
@RestController
@RequestMapping("/api/fufenAmusementPark")
public class FufenAmusementParkController extends CommonCtrl {

    @Autowired
    private ScoreRecordTotalService recordTotalService;
    @Autowired
    private AgentScoreRuleService agentScoreRuleService;
    @Autowired
    private ScoreTotalService scoreTotalService;
    @Autowired
    private AgentInfoService agentInfoService;
    @Autowired
    private AgentAppService agentAppService;

    /**
     * 福分商城统一接口
     */
    @PostMapping("")
    public ApiRes getAmusementPark() {
        JSONObject jsonObject = new JSONObject();
        Long fansId = getCurrentUser().getFansId();
        String appId = getCurrentUser().getAppId();
        String infoType = Constants.SCORE.RULE; //getValString("infoType");
        String time = getValString("time");
        log.info("福分商城统一接口请求参数，请求地址：/api/fufenAmusementPark，请求参数：appId:{},userId:{},infoType:{},time:{}",
                appId, fansId, infoType, time);
        if (StringUtils.isEmpty(appId) || Objects.isNull(fansId) || StringUtils.isEmpty(infoType) ||
                StringUtils.isEmpty(time)) {
            return ApiRes.customFail("网络故障，请稍后重试！");
        }
        List<AgentScoreRuleEntity> getList = agentScoreRuleService.scoreRuleList(appId);
        jsonObject.put("ruleList", getList);//获取积分规则
        Long ruleId = 1L;
        for (AgentScoreRuleEntity ruleEntity : getList) {
            if (ruleEntity.getRuleKey().equals("sign")) {
                ruleId = ruleEntity.getScoreRuleId();//获取签到规则
            }
        }

        List<ScoreRecordTotalEntity> getSignList = recordTotalService.findSignRecordList(fansId, ruleId, infoType, time);
        jsonObject.put("getSignList", getSignList);//获取签到记录

        JSONObject checkSign = recordTotalService.checkSign(fansId, ruleId, Constants.SCORE.RULE);
        jsonObject.put("checkSign", checkSign);//检验签到

        int getUserScoreSum = recordTotalService.getUserScoreSum(fansId, appId);
        jsonObject.put("userScoreSum", getUserScoreSum);//获取当天获得福分总量

        ScoreTotalEntity scoreTotalEntity = scoreTotalService.selectByFansId(fansId, appId);
        if (scoreTotalEntity != null) {
            jsonObject.put("score", scoreTotalEntity.getCurrentScore());
        } else {
            jsonObject.put("score", 0);
        }//获取积分数量
        log.info("福分商城统一接口返回参数，请求地址：/api/fufenAmusementPark，返回参数：{}", JSONUtil.toJsonStr(jsonObject));
        return ApiRes.ok(jsonObject);
    }


}
